import hashlib
import json
import logging
from utils.constants import *
from Input.qqInput import QqInput
from Input.wechatInput import WechatInput
from Model.messageQueue import MessageQueue


logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
messageQueue = MessageQueue()

def load_config(file_path='config.json'):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
            return data
    except FileNotFoundError:
        print("错误: 文件未找到!")
    except json.JSONDecodeError:
        print("错误: 文件不是有效的 JSON 格式!")
    except Exception as e:
        print(f"错误: 发生了一个未知错误: {e}")
    return None

def get_instance(config):
    if config[SOFTWARE] == '微信':
        return WechatInput(config, messageQueue)
    elif config[SOFTWARE] == 'QQ':
        return QqInput(config, messageQueue)


import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication


def send_email(sender_email, sender_password, receiver_email, subject, body, attachment_paths):
    # 创建一个 MIMEMultipart 对象
    message = MIMEMultipart()
    message["From"] = sender_email
    message["To"] = receiver_email
    message["Subject"] = subject

    # 添加邮件正文
    message.attach(MIMEText(body, "plain"))

    # 打开附件文件
    for attachment_path in attachment_paths:
        try:
            with open(attachment_path, "rb") as file:
                # 创建附件对象
                attachment = MIMEApplication(file.read())
                # 设置附件的文件名
                attachment.add_header('Content-Disposition', 'attachment', filename=attachment_path.split("/")[-1])
                # 将附件添加到邮件中
                message.attach(attachment)
        except FileNotFoundError:
            print(f"错误: 未找到附件文件 {attachment_path}")

    try:
        # 连接到 SMTP 服务器
        server = smtplib.SMTP("smtp.example.com", 587)  # 根据你的邮箱提供商修改
        # 启用 TLS 加密
        server.starttls()
        # 登录发件人邮箱
        server.login(sender_email, sender_password)
        # 发送邮件
        server.sendmail(sender_email, receiver_email, message.as_string())
        # 关闭连接
        server.quit()
        print("邮件发送成功！")
    except Exception as e:
        print(f"邮件发送失败: {e}")


# # 使用示例
# sender_email = "your_email@example.com"
# sender_password = "your_email_password"
# receiver_email = "recipient_email@example.com"
# subject = "测试邮件 - 含附件"
# body = "这是一封包含附件的测试邮件。"
# attachment_path = "path/to/your/attachment.pdf"  # 替换为实际的附件路径
#
# send_email(sender_email, sender_password, receiver_email, subject, body, attachment_path)
